package com.czmini.cztest.controller;

import com.alibaba.fastjson.JSONObject;
import com.czmini.cztest.common.Token;
import com.czmini.cztest.common.TokenDb;
import com.czmini.cztest.config.UserBaseStr;
import com.czmini.cztest.dto.*;
import com.czmini.cztest.dto.user.AddUserDto;
import com.czmini.cztest.dto.user.LoginUserDto;
import com.czmini.cztest.entity.ZcTestUser;
import com.czmini.cztest.service.ZcTestUserService;
import org.springframework.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.controller
 * @ClassName: ZcTestUserController
 * @Author: 还是那个橙子
 * @Description:
 * @Date: 2022/3/2 23:26
 * @Version: 1.0
 */
 //RestController 语义级别的注解
@RestController  //将类标记成对外服务的web类  spring会转换返回值并自动写入到http响 应中
@RequestMapping("zctest")
@Api(tags = " zctest-用户模板")
@Slf4j
public class ZcTestUserController {

    @Autowired
    private TokenDb tokenDb;
    @Autowired
    private ZcTestUserService zcTestUserService;

/*    @Value("${zczc.key1}")
    private  String zckey1;*/


    @ApiOperation("用户注册")
    @PostMapping("register") //
    public ResultDto<ZcTestUser> register(@RequestBody AddUserDto addUserDto){    //将request body中的json/xml对象解析成该参数类型的Javabean对
        log.info("用户注册入参"+addUserDto);
        if(Objects.isNull(addUserDto)){
            return ResultDto.success("用户信息不能为空");
        }
        String userName = addUserDto.getUserName();
        if(StringUtils.isEmpty(userName)){
            return ResultDto.success("用户名不能为空");
        }
        String pwd = addUserDto.getPassword();
        if(StringUtils.isEmpty(pwd)){
            return ResultDto.success("密码不能为空");
        }
        // 数据库操作类
        ZcTestUser zctestUser = new ZcTestUser();
        // 将数据库dto类 复制给实体类
        BeanUtils.copyProperties(addUserDto,zctestUser);
        ResultDto<ZcTestUser> resultDto = zcTestUserService.save(zctestUser);
        return resultDto;
    }

    @ApiOperation("登录接口")
    @RequestMapping(value ="login",method = RequestMethod.POST) // 处理http响
    public ResultDto<Token> login(@RequestBody LoginUserDto loginUserDto){    //将request body中的json/xml对象解析成该参数类型的Javabean对象
        // 设置自定义异常
        log.info("用户登录-入参= "+ JSONObject.toJSONString(loginUserDto));

        String userName = loginUserDto.getUserName();
        String password = loginUserDto.getPassword();
        log.info("userName= "+userName);
        if(StringUtils.isEmpty(userName)|| StringUtils.isEmpty(password)){
            return ResultDto.fail("用户名或密码不能为空");
        }
        System.out.println("baocao=============================");
        ResultDto<Token> resultDto = zcTestUserService.login(userName, password);

        return resultDto;
    }

    @GetMapping("isLogin")
    public ResultDto islogin(HttpServletRequest request) {    //将request body中的json/xml对象解析成该参数类型的Javabean对象
        // 从请求头获取token
        String tokenStr = request.getHeader(UserBaseStr.LOGIN_TOKEN);
        TokenDto userInfo = tokenDb.getTokenDto(tokenStr);
        System.out.println(userInfo.getUserId());
        return ResultDto.success("成功"+userInfo);
    }


    @ApiOperation("修改接口")
    @PutMapping //
    public ResultDto<AddUserDto> update (@RequestBody UpdateTestUserDto updateTestUserDto){    //将request body中的json/xml对象解析成该参数类型的Javabean对象
        // 数据库操作类
        ZcTestUser zctestUser = new ZcTestUser();
        // 将数据库dto类 复制给实体类
        BeanUtils.copyProperties(updateTestUserDto,zctestUser);

        log.info("注册请求入参"+JSONObject.toJSONString(zctestUser));

        return ResultDto.success(" success"+ zcTestUserService.update(zctestUser));
    }
    // 查找数据
    @RequestMapping(value ="byid/{id}/{miniid}",method = RequestMethod.GET) // 处理http响
    public String get1(@PathVariable("id") Long id,   //pathvariable 处理动态rui 值可以作为控制器中的处理方法的参数
                       @PathVariable(value = "miniid",required = false)Long miniid){  //false 为非必填
        System.out.println("pwd"+id+miniid);
        return " success";
    }


    @GetMapping("byname") // 返回数组
    public ResultDto<List<ZcTestUser>> getByName(@RequestParam(value = "userId",required = false) Integer userId,
                                     @RequestParam(value = "userName",required = false) String userName){
        ZcTestUser zcTestUser = new ZcTestUser();
        zcTestUser.setId(userId);
        zcTestUser.setUserName(userName);
        log.info("用户名模糊查询"+JSONObject.toJSONString(zcTestUser));


         return zcTestUserService.ByName(zcTestUser);
    }

    //处理get请求参数
    @RequestMapping(value ="byid",method = RequestMethod.GET) // 处理http响
    public String get2(@RequestParam("id") Long id,   //pathvariable 处理动态rui 值可以作为控制器中的处理方法的参数
                       @RequestParam(value = "miniid",required = false)Long miniid){  //false 为非必填  http://localhost:8081/zctest/byid?id=21&minid=32
        System.out.println("RequestParam"+id+miniid);
        return " success";
    }

    @ApiOperation("删除接口")
    @DeleteMapping("{userId}") //删除的用户id
    public ResultDto delete(@PathVariable("userId")Integer userId){
        System.out.println("用户id删除信息"+userId);
        return zcTestUserService.delete(userId);
    }
    @ApiOperation(value = "登出接口")
    @DeleteMapping("/logout")
    public ResultDto logout(HttpServletRequest request){
        // 获取token
        String token = request.getHeader(UserBaseStr.LOGIN_TOKEN);
          // 获取登录标识
        boolean flag = tokenDb.isLogin(token);
        if (!flag){
            return ResultDto.fail("未登录");
        }
        TokenDto tokenDto = tokenDb.removeTokenDto(token);

        return  ResultDto.success("成功",tokenDto);
    }


}
